package com.sc.es.controller.fe;

import com.sc.es.common.ApiResponse;
import com.sc.es.common.HouseResult;
import com.sc.es.common.ServiceResult;
import com.sc.es.dto.HouseBucketDTO;
import com.sc.es.entity.House;
import com.sc.es.form.MapSearch;
import com.sc.es.search.ISearchService;
import com.sc.es.service.IAddressService;
import com.sc.es.service.IHouseService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author sc
 * Created on  2018/2/23
 */
@Controller
public class HouseMapController {

    @Autowired
    private IAddressService addressService;
    @Autowired
    private ISearchService searchService;
    @Autowired
    private IHouseService houseService;

    @GetMapping("/rent/house/map")
    public String rentHouseMapPage(@RequestParam("cityEnName")String cityEnName,
                                   Model model, HttpSession session,
                                   RedirectAttributes redirectAttributes){
        HouseResult city = addressService.findCity(cityEnName);
        if(!city.isSuccess()){
            redirectAttributes.addAttribute("msg","must_choose_city");
            return "redirect:/index";
        }else {
            session.setAttribute("cityEnName",cityEnName);
            model.addAttribute("city",city.getResult());
        }
        HouseResult houses = houseService.findHouseInCity(cityEnName);
        ServiceResult<HouseBucketDTO> result = searchService.aggCityHouse(cityEnName);

        ServiceResult regions = addressService.findAllRegionsByCity(cityEnName);
        model.addAttribute("houses",houses.getResult());
        model.addAttribute("total",result.getTotal());
        model.addAttribute("aggData",result.getResult());
        model.addAttribute("regions",regions.getResult());
        return "rent-map";
    }

    @GetMapping("/rent/house/map/houses")
    @ResponseBody
    public ApiResponse rentMapHouses(@ModelAttribute MapSearch mapSearch){
        if(mapSearch.getCityEnName()==null){
            return ApiResponse.ofMessage(HttpStatus.BAD_REQUEST.value(),"必须选择城市");
        }
        ServiceResult result;
        if(mapSearch.getLevel()<13){
            result = houseService.wholeMapQuery(mapSearch);
        } else{
            result=houseService.boundMapQuery(mapSearch);
        }
        ApiResponse response = ApiResponse.ofSuccess(result.getResult());
        response.setMore(result.getTotal()>(mapSearch.getStart()+mapSearch.getSize()));
        return  response;
    }
}
